package com.cta.tcp.server;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;

import javax.jms.JMSException;

import com.cta.jms.MessageProducer;

public class TestingService {

	public static String TEST_STR = "100912 131923 0123 %s    204 ";
	static String[] areaCodes = "2 3 4 5 6 7 090 0183 0188 093 0122 091 094 8 9 0123 098 097 0126 0168 0169 092 095 096 018 0193 0198 019 0203 0208 020 02103 02108 0210 02113 02118 0211 0223 0228 022 0233 0238 023 02403 02408 0240 02413 02418 0241 0253 0258 025 0263 0268 026 0273 0278 027 02803 02808 0280 02813 02818 0281 0293 0298 029 0303 0308 030 0313 0318 031 03203 03208 0320 03213 03218 0321 0333 0338 033 0343 0348 034 03503 03508 0350 03513 03518 0351 0363 0368 036 0373 0378 037 0383 0388 038 0393 0398 039 043 048 04 0503 0508 050 05103 05108 0510 05113 05118 0511 0523 0528 052 0533 0538 053 0543 0548 054 0553 0558 055 0563 0568 056 0573 0578 057 0583 0588 058 0593 0598 059 0603 0608 060 0613 0618 061 0623 0628 062 0633 0638 063 0643 0648 064 06503 06508 0650 06513 06518 0651 0663 0668 066 0673 0678 067 0683 0688 068 0703 0708 070 0713 0718 071 0723 0728 072 0733 0738 073 0743 0748 074 0753 0758 075 0763 0768 076 0773 0778 077 07803 07808 0780 07813 07818 0781 0793 0798 079 083 088 08 1710183 1710188 171018 1710193 1710198 171019 1710203 1710208 171020 17102103 17102108 1710210 17102113 17102118 1710211 1710223 1710228 171022 1710233 1710238 171023 17102403 17102408 1710240 17102413 17102418 1710241 1710253 1710258 171025 1710263 1710268 171026 1710273 1710278 171027 17102803 17102808 1710280 17102813 17102818 1710281 1710293 1710298 171029 1710303 1710308 171030 1710313 1710318 171031 17103203 17103208 1710320 17103213 17103218 1710321 1710333 1710338 171033 1710343 1710348 171034 17103503 17103508 1710350 17103513 17103518 1710351 1710363 1710368 171036 1710373 1710378 171037 1710383 1710388 171038 1710393 1710398 171039 171043 171048 17104 1710503 1710508 171050 17105103 17105108 1710510 17105113 17105118 1710511 1710523 1710528 171052 1710533 1710538 171053 1710543 1710548 171054 1710553 1710558 171055 1710563 1710568 171056 1710573 1710578 171057 1710583 1710588 171058 1710593 1710598 171059 1710603 1710608 171060 1710613 1710618 171061 1710623 1710628 171062 1710633 1710638 171063 1710643 1710648 171064 17106503 17106508 1710650 17106513 17106518 1710651 1710663 1710668 171066 1710673 1710678 171067 1710683 1710688 171068 1710703 1710708 171070 1710713 1710718 171071 1710723 1710728 171072 1710733 1710738 171073 1710743 1710748 171074 1710753 1710758 171075 1710763 1710768 171076 1710773 1710778 171077 17107803 17107808 1710780 17107813 17107818 1710781 1710793 1710798 171079 171083 171088 17108 1710183 1710188 1710193 1710198 1710203 1710208 17102103 17102108 17102113 17102118 1710223 1710228 1710233 1710238 17102403 17102408 17102413 17102418 1710253 1710258 1710263 1710268 1710273 1710278 17102803 17102808 17102813 17102818 1710293 1710298 1710303 1710308 1710313 1710318 17103203 17103208 17103213 17103218 1710333 1710338 1710343 1710348 17103503 17103508 17103513 17103518 1710363 1710368 1710373 1710378 1710383 1710388 1710393 1710398 171043 171048 1710503 1710508 17105103 17105108 17105113 17105118 1710523 1710528 1710533 1710538 1710543 1710548 1710553 1710558 1710563 1710568 1710573 1710578 1710583 1710588 1710593 1710598 1710603 1710608 1710613 1710618 1710623 1710628 1710633 1710638 1710643 1710648 17106503 17106508 17106513 17106518 1710663 1710668 1710673 1710678 1710683 1710688 1710703 1710708 1710713 1710718 1710723 1710728 1710733 1710738 1710743 1710748 1710753 1710758 1710763 1710768 1710773 1710778 17107803 17107808 17107813 17107818 1710793 1710798 171083 171088 0128 0120 0125 0127 0166 0167 0165"
			.split(" ");

	public static String getDialNumber(String appendNum) {
		Random rand = new Random();
		int min = 1, max = areaCodes.length - 1;
		int randomNum = rand.nextInt(max - min + 1) + min;
		String code = areaCodes[randomNum].trim() + appendNum;
		if (code.length() < 23) {
			String subCode = "";
			for (int i = 0; i < 23 - code.length(); i++) {
				subCode += " ";
			}
			code = subCode + code;
		}
		return code.substring(0, 22);
	}

	public static String getData() {
		String data = String.format(TEST_STR, getDialNumber("12345"));
		return data;
	}

	public static void sentDataFromfile(MessageProducer producer) throws FileNotFoundException, InterruptedException, JMSException {
		Scanner scanner = new Scanner(new FileInputStream("d:/Working/cdr/my-billing.txt"));
		try {
			while (scanner.hasNextLine()) {
				Thread.sleep(1000);
				producer.sendMessages(scanner.nextLine());
			}
		} finally {
			scanner.close();
		}
	}
}
